package com.xbq.exceleditor.utils

import android.app.Activity
import android.content.Context
import android.view.View
import androidx.fragment.app.Fragment
import com.blankj.utilcode.util.ResourceUtils
import com.hjq.permissions.Permission
import com.xbq.xbqsdk.core.ext.ensureFeature
import com.xbq.xbqsdk.core.ext.ensureLogin
import com.xbq.xbqsdk.net.common.vo.LoginVO
import com.xbq.xbqsdk.net.userCache
import com.xbq.xbqsdk.util.click.onDebouncedClick
import com.xbq.xbqsdk.util.ensurePermission
import java.io.File

/**
 * Author: liaohaiping
 * Time: 2020-11-12
 * Description:
 */


fun Activity.ensureVip(where: String, callback: () -> Unit) =
    ensureFeature(FeatureEnum.EXCEL_EDITOR.name, where, callback)


fun Activity.ensureVip(where: String, needVip: Boolean, callback: () -> Unit) {
    if (needVip) {
        ensureFeature(FeatureEnum.EXCEL_EDITOR.name, where, callback)
    } else {
        callback()
    }
}


fun Fragment.ensureVip(where: String, callback: () -> Unit) =
    ensureFeature(FeatureEnum.EXCEL_EDITOR.name, where, callback)

fun Fragment.ensureVip(where: String, needVip: Boolean, callback: () -> Unit) {
    if (needVip) {
        ensureFeature(FeatureEnum.EXCEL_EDITOR.name, where, callback)
    } else {
        callback()
    }
}

fun View.ensureLoginClick(activity: Activity, callback: () -> Unit) =
    onDebouncedClick {
        activity.ensureLogin(callback)
    }

fun View.ensureVipClick(activity: Activity, where: String, callback: () -> Unit) =
    onDebouncedClick { activity.ensureVip(where, callback) }

fun View.ensureLoginClick(fragment: Fragment, callback: () -> Unit) = onDebouncedClick {
    fragment.ensureLogin(callback)
}

fun View.ensureVipClick(fragment: Fragment, where: String, callback: () -> Unit) =
    onDebouncedClick {
        fragment.ensureVip(where, callback)
    }

fun Fragment.ensureStoragePermission(message: String, callback: () -> Unit) = ensurePermission(
    permissions = arrayOf(Permission.MANAGE_EXTERNAL_STORAGE),
    message = message,
    onAllGranted = callback
)

fun Activity.ensureStoragePermission(message: String, callback: () -> Unit) = ensurePermission(
    permissions = arrayOf(Permission.MANAGE_EXTERNAL_STORAGE),
    message = message,
    onAllGranted = callback
)

fun LoginVO.formatFeatures(): String {
    return if (userFeatures != null && userFeatures.size > 0) {
        userFeatures.joinToString("\n") { it.formatSimpleFeature() }
    } else ""
}

fun userCache.isFreeOrVip(): Boolean = isFreeOrCanUse(FeatureEnum.EXCEL_EDITOR.name)
fun userCache.isVip(): Boolean = canUse(FeatureEnum.EXCEL_EDITOR.name)

fun Context.getTemplateDir(): File {
    var dir = getExternalFilesDir("template")
    if (dir == null) {
        dir = File(filesDir, "template")
    }
    dir.mkdirs()
    return dir
}
fun Context.ensureCopyFile(xlsxThumbnail:String):File {
    var xlsx = xlsxThumbnail.replace(".png", ".xlsx")
    val xlsxFile = File(getTemplateDir(), xlsx)
    if (xlsxFile.exists() == false) {
        ResourceUtils.copyFileFromAssets("templates/${xlsx}", xlsxFile.absolutePath)
    }
    return xlsxFile
}